package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShShopCartDao;
import com.shop.shoppingmall.entity.ShShopCart;
import com.shop.shoppingmall.service.ShShopCartService;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 购物车(ShShopCart)表服务实现类
 *
 * @author makejava
 * @since 2020-09-09 11:39:54
 */
@Service("shShopCartService")
@Transactional
public class ShShopCartServiceImpl implements ShShopCartService {
    @Resource
    private ShShopCartDao shShopCartDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShShopCart queryById(String id) {
        return this.shShopCartDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShShopCart> queryAllByLimit(int offset, int limit) {
        return this.shShopCartDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shShopCart 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(ShShopCart shShopCart) {
        shShopCart.setId(UUIDGenerator.generate());
        ShShopCart shShopCart1 = shShopCartDao.queryBySpecsId(shShopCart.getSpecsId(),shShopCart.getUserId());
        if(shShopCart1 != null){
            shShopCart1.setNums(shShopCart1.getNums()+shShopCart.getNums());
            shShopCartDao.update(shShopCart1);
            return  Result.ok("添加成功");
        }else{
            shShopCartDao.insert(shShopCart);
            return  Result.ok("添加成功");
        }

    }

    /**
     * 修改数据
     *
     * @param shShopCart 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(ShShopCart shShopCart) {
            shShopCartDao.update(shShopCart);
            return  Result.ok("修改成功");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(String id) {
        shShopCartDao.deleteById(id);
        return  Result.ok("已删除");
    }

    @Override
    public Result queryUserId(String userId) {
        List<ShShopCart> shShopCarts = shShopCartDao.queryUserId(userId);
        for (ShShopCart shShopCart : shShopCarts){
            shShopCart.setProductPrice(shShopCart.getProductPrice().add(shShopCart.getRebatePrice()));
        }
        return  Result.ok(shShopCarts);
    }

    @Override
    public Result deleteBatchByParams(List<String> ids) {
        shShopCartDao.deleteBatchByParams(ids);
        return  Result.ok("已删除");
    }
}
